<template>
  <panel-head :route="route"></panel-head>
  <div class="btns">
    <el-button type="primary" size="small" :icon="Plus" @click="open(null)"
      >新增</el-button
    >
  </div>

  <el-table style="width: 100%; margin-top: 10px" :data="tableData.list">
    <el-table-column prop="id" label="id" />
    <el-table-column prop="name" label="昵称" />
    <el-table-column prop="permissionName" label="菜单权限" width="500px" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination
      v-model:current-page="paginationData.pageNum"
      :page-size="paginationData.pageSize"
      :size="small"
      :background="false"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <el-dialog
    v-model="dialogFromVisible"
    width="500"
    title="添加权限"
    :before-close="dialogBeforeClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="100px"
      label-position="left"
      :rules="rules"
    >
      <el-form-item prop="id" v-show="false">
        <el-input v-model="form.id" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请填写权限名称" />
      </el-form-item>

      <el-form-item label="权限" prop="permissions">
        <el-tree
          ref="treeRef"
          :data="permissionData"
          style="max-width: 600px"
          node-key="id"
          show-checkbox
          :default-checked-keys="defaultKeys"
          :default-expanded-keys="[2]"
        >
        </el-tree>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-foorter">
        <el-button @click="dialogFromVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm(formRef)">确 定</el-button>
      </div>
    </template>
    <div></div>
  </el-dialog>
</template>
<script setup>
import { ref, reactive, onMounted, nextTick } from "vue";
import { userGetMenu, userSetMenu, menuList } from "../../../api";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import { useRoute } from "vue-router";
const route = useRoute();
onMounted(async () => {
  await userGetMenu().then((data) => {
    permissionData.value = data.data;
    getListData();
  });
});

const paginationData = reactive({
  pageNum: 1,
  pageSize: 10,
});
const handleSizeChange = (val) => {
  paginationData.pageSize = val;
  getListData();
};
const handleCurrentChange = (val) => {
  paginationData.pageNum = val;
  getListData();
};
// 打开弹窗
const open = (rowData = {}) => {
  dialogFromVisible.value = true;
  // 弹窗打开form生成是异步的
  nextTick(() => {
    if (rowData) {
      Object.assign(form, { id: rowData.id, name: rowData.name });
      console.log(rowData.permissions, "测试");
      treeRef.value.setCheckedKeys(rowData.permission);
    }
  });
};
// 列表数据
const tableData = reactive({
  list: [],
  total: 0,
});
// 请求列表数据
const getListData = () => {
  menuList(paginationData).then(({ data }) => {
    const { list, total } = data;
    tableData.list = list;
    tableData.total = total;
  });
};

const formRef = ref();

// 权限树形结构菜单 数据
const permissionData = ref([]);
// form的数据
const form = reactive({
  id: "",
  name: "",
  permissions: "",
});
// 控制弹窗显示隐藏
const dialogFromVisible = ref(false);
// 关闭弹窗的回调
const dialogBeforeClose = () => {
  dialogFromVisible.value = false;
  // 重置表单
  formRef.value.resetFields();
  // tree选择重置
  treeRef.value.setCheckedKeys(defaultKeys);
};
const defaultKeys = [4, 5];
const treeRef = ref();
// 表单提交
const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 获取到选择的checkbox数据
      const permissions = JSON.stringify(treeRef.value.getCheckedKeys());
      userSetMenu({ name: form.name, permissions, id: form.id }).then(
        (data) => {
          ElMessage.success(data.data);
          dialogBeforeClose();
          getListData();
        }
      );
    } else {
      console.log("error submit", fields);
    }
  });
};
const rules = reactive({
  name: [
    {
      required: true,
      trigger: "blur",
      message: "请输入权限名称",
    },
  ],
});
</script>
<style scoped lang="less">
.btns {
  padding: 10px 0 10px 10px;
  background-color: #fff;
}
</style>
